class Register {
    constructor() {
        // 登录按钮绑定点击事件
        this.$('#button').addEventListener('click', this.checkRegister)
    }

    /*****注册*****/
    checkRegister = () => {
        // console.log(1111);
        // 获取表单中的数据
        let form = document.forms.register.elements;
        // console.log(form);
        let nickname = form.nickname.value;
        let username = form.username.value;
        let password = form.password.value;
        let rpassword = form.rpassword.value;
        // console.log(username, password, rpassword);

        // 非空验证
        if (!password || !username || !rpassword || !nickname) {
            alert('输入错误');
            return;
        }

        // 设置编码方式
        axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded';

        // 发送ajax请求，注册
        let param = `username=${username}&password=${password}&rpassword=${rpassword}&nickname=${nickname}`;
        axios.post('http://localhost:8888/users/register', param).then(({ status, data }) => {
            // console.log(data);
            // 判断是否注册成功
            if (status != 200 || data.code != 1) {
                alert('注册失败');
                return;
            } else {
                alert('注册成功')
            }

            // 回跳到登录页面
            location.assign('../html/login.html')
        })
    }

    // 获取节点的方法
    $(ele) {
        let res = document.querySelectorAll(ele);
        return res.length == 1 ? res[0] : res;
    }
}

new Register;